<?php

define("FETCH_NUM", 1);
define("FETCH_ASSOC", 2);
define("FETCH_BOTH", 3);

class internode_api
{
	var $username = NULL;
	var $password = NULL;
	
	var $internode_version = "12";
	
	var $api_url = "https://customer-webtools-api.internode.on.net/cgi-bin/padsl-usage";
	var $internode_login = "/cgi-bin/login";

	var $info = null;
	var $history = null;

	function __construct()
	{
		// Check installation options.
		//
		if(!function_exists('curl_init'))
			trigger_error("Your PHP installation is missing the CURL extension.", E_USER_ERROR);
		
		if(!CURLOPT_SSLVERSION)
			trigger_error("Your CURL version does not have SSL support enabled.", E_USER_ERROR);
		
//		$history = $this->fetch_data(INTERNODE_HISTORY);
	}
	
	function get_info($prefer = FETCH_ASSOC)
	{
		if(!$this->info)
		{
			$keys = array('used', 'quota', 'date_rollover');
			foreach( explode( " ", trim( $this->fetch_data() ) ) as $i => $v )
			{
				if( is_numeric($v) )
					$v = $v * 1;
				
				if( isset($keys[$i]) && ($prefer & FETCH_ASSOC) )
					$this->info[$keys[$i]] = $v;
				
				if( !isset($keys[$i]) || ($prefer & FETCH_NUM) )
				$this->info[$i] = $v;
			}
		}
		return $this->info;
	}
	
	function get_unlimited()
	{
		return ($this->quota < 1);
	}
	
	function get_quota_remaining()
	{
		$this->get_info();
		return $this->info['quota'] - $this->info['used'];
	}
	
	function get_quota_percentage()
	{
		$this->get_info();
		return 100 * $this->info['used'] / $this->info['quota'];
	}
	
	function get_days_remaining()
	{
		$this->get_info();
		list($y,$m,$d) = sscanf($this->info['date_rollover'], "%04d%02d%02d");
		return intval( (strtotime( sprintf("%04d-%02d-%02d 00:00:00 +1000", $y, $m, $d)) - time()) / (60*60*24));
	}
	
	function __get($name)
	{
		$this->get_info();
		
//		var_dump(method_exists(__CLASS__,"get_$name"));
		
		if( isset($this->info[$name]) )
			return $this->info[$name];
		elseif( method_exists(__CLASS__,"get_$name") )
		{
			$method = "get_$name";
			return $this->$method();
		}
		else
			return "error '$name'";
	}
	
	function time_from_format($format, $str)
	{
		$a = date_parse_from_format($format, $str);
		return mktime(
			$a['hour'],
			$a['minute'],
			$a['second'],
			$a['month'],
			$a['day'],
			$a['year']		);
	}
	
	function get_history()
	{
		if(!$this->history)
		{
			foreach( explode( "\n", trim( $this->fetch_data( array('history'=>1) ) ) ) as $line )
			{
				list($k, $v) = explode(" ", $line);
				$this->history[] = array('date'=>$this->time_from_format('ymd', $k), 'usage' => $v*1);
			}
//			$this->history = $history;
		}
		return $this->history;
	}
	
	function fetch_data($post=array())
	{
		$post['username'] = $this->username; // ."@internode.on.net"
		$post['password'] = $this->password;
		$post['iso'] = 1;
		//$post['history'] = 1;
		
		$o = curl_init();
		
		if( parse_url( $this->api_url, PHP_URL_SCHEME) == 'https' )
		{
			curl_setopt($o, CURLOPT_SSL_VERIFYPEER, 0);
			curl_setopt($o, CURLOPT_SSL_VERIFYHOST, 0);
		}
		
		curl_setopt($o, CURLOPT_URL, $this->api_url);
		curl_setopt($o, CURLOPT_RETURNTRANSFER, 1);
		
		curl_setopt($o, CURLOPT_POST, 1);
		curl_setopt($o, CURLOPT_POSTFIELDS, $post );
		
		curl_setopt($o, CURLOPT_USERAGENT, sprintf("internode.php v.%d; Copyright 2004-2008 Intellectual Property Holdings Pty. Ltd.", $this->internode_version ) );
		
		$result = curl_exec($o); // run the whole process
		
		if(!$result)
			$result = "CURL Error ". curl_errno($o) .": ". curl_error($o);
		curl_close($o);
		
		return $result;
	}
}
